import Mock from 'mockjs'

// 设置响应延时
Mock.setup({
  timeout: '200-600',
})

// 创建待办事项的接口
Mock.mock('/api/todo-item/create', 'post', (options) => {
  const { content, labels } = JSON.parse(options.body)

  // 这里可以添加一些验证逻辑
  if (!content || !labels || !labels.length) {
    return {
      success: false,
      code: '40001',
      msg: '参数错误',
      data: null,
    }
  }

  // 模拟生成一个新的待办事项ID
  const newTodoId = Mock.Random.integer(1000, 9999)

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: newTodoId,
  }
})

// 更新待办事项的接口
Mock.mock('/api/todo-item/update', 'post', (options) => {
  const { id } = JSON.parse(options.body)

  // 验证参数
  if (!id) {
    return {
      success: false,
      code: '40001',
      msg: '待办事项ID不能为空',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 创建标签的接口
Mock.mock('/api/label/create', 'post', (options) => {
  const { name } = JSON.parse(options.body)

  // 验证参数
  if (!name || !name.trim()) {
    return {
      success: false,
      code: '40001',
      msg: '标签名称不能为空',
      data: null,
    }
  }

  // 模拟生成一个新的标签ID
  const newLabelId = Mock.Random.integer(1000, 9999)

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: newLabelId,
  }
})

// 删除标签
Mock.mock('/api/label/remove', 'post', (options) => {
  const { id } = JSON.parse(options.body)

  if (!id) {
    return {
      success: false,
      code: '10001',
      msg: '标签ID不能为空',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 更新标签
Mock.mock('/api/label/update', 'post', (options) => {
  const { id, name } = JSON.parse(options.body)

  if (!id) {
    return {
      success: false,
      code: '10001',
      msg: '标签ID不能为空',
      data: null,
    }
  }

  if (!name || !name.trim()) {
    return {
      success: false,
      code: '10002',
      msg: '标签名称不能为空',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 获取所有标签
Mock.mock('/api/label/get-all', 'get', () => {
  // 模拟一些标签数据
  const labels = [
    { id: 1, name: '工作', order: 0 },
    { id: 2, name: '生活', order: 1 },
    { id: 3, name: '学习', order: 2 },
    { id: 4, name: '测试', order: 3 },
    { id: 5, name: '测试', order: 4 },
    { id: 6, name: '测试', order: 5 },
    { id: 7, name: '测试', order: 6 },
  ]

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: labels,
  }
})

// 获取展示的标签
Mock.mock('/api/label/get-shown', 'get', () => {
  // 模拟一些标签数据
  const labels = [
    { id: 1, name: '工作1', order: 0 },
    { id: 2, name: '生活2', order: 1 },
    { id: 3, name: '学习3', order: 2 },
  ]

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: labels,
  }
})

// 获取展示中的待办事项列表
Mock.mock('/api/todo-item/get-shown', 'get', () => {
  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: [
      {
        labelId: 1,
        labelName: '工作1',
        todoItems: [
          {
            todoItemId: 101,
            content: '完成产品设计文档',
            createdAt: '2023-03-15 09:30:00',
            updatedAt: '2023-03-15 10:15:00',
          },
          {
            todoItemId: 102,
            content: '修复前端Bug',
            createdAt: '2023-03-15 11:20:00',
            updatedAt: '2023-03-15 11:20:00',
          },
        ],
      },
      {
        labelId: 2,
        labelName: '生活2',
        todoItems: [
          {
            todoItemId: 201,
            content: '购买生活用品',
            createdAt: '2023-03-15 14:30:00',
            updatedAt: '2023-03-15 14:30:00',
          },
          {
            todoItemId: 202,
            content: '健身1小时',
            createdAt: '2023-03-15 16:00:00',
            updatedAt: '2023-03-15 16:00:00',
          },
          {
            todoItemId: 203,
            content: '阅读新书',
            createdAt: '2023-03-15 20:00:00',
            updatedAt: '2023-03-15 20:00:00',
          },
        ],
      },
      {
        labelId: 3,
        labelName: '学习3',
        todoItems: [
          {
            todoItemId: 301,
            content: '学习Vue 3新特性',
            createdAt: '2023-03-15 08:00:00',
            updatedAt: '2023-03-15 08:00:00',
          },
          {
            todoItemId: 302,
            content: '复习TypeScript',
            createdAt: '2023-03-15 13:30:00',
            updatedAt: '2023-03-15 13:30:00',
          },
        ],
      },
    ],
  }
})

// 添加标签到展示列表
Mock.mock('/api/label/add-show', 'post', (options) => {
  const { id } = JSON.parse(options.body)

  if (!id) {
    return {
      success: false,
      code: '10001',
      msg: '标签ID不能为空',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 更新展示标签排序
Mock.mock('/api/label/update-show-sort', 'post', (options) => {
  const idArray = JSON.parse(options.body)

  if (!Array.isArray(idArray) || idArray.length === 0) {
    return {
      success: false,
      code: '10001',
      msg: '无效的标签ID数组',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 获取已完成的待办事项
Mock.mock('/api/todo-item/get-completed', 'get', () => {
  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: [
      {
        labelId: 1,
        labelName: '工作',
        todoItems: [
          {
            todoItemId: 111,
            content: '完成项目演示文档',
            createdAt: '2023-03-10 14:30:00',
            updatedAt: '2023-03-12 16:45:00',
            completedAt: '2023-03-12 16:45:00',
          },
          {
            todoItemId: 112,
            content: '进行团队周会',
            createdAt: '2023-03-08 09:00:00',
            updatedAt: '2023-03-08 10:30:00',
            completedAt: '2023-03-08 10:30:00',
          },
        ],
      },
      {
        labelId: 2,
        labelName: '生活',
        todoItems: [
          {
            todoItemId: 211,
            content: '健身30分钟',
            createdAt: '2023-03-11 18:00:00',
            updatedAt: '2023-03-11 19:00:00',
            completedAt: '2023-03-11 19:00:00',
          },
          {
            todoItemId: 212,
            content: '购买周末食材',
            createdAt: '2023-03-10 17:00:00',
            updatedAt: '2023-03-10 18:30:00',
            completedAt: '2023-03-10 18:30:00',
          },
          {
            todoItemId: 213,
            content: '读完《原子习惯》',
            createdAt: '2023-03-01 20:00:00',
            updatedAt: '2023-03-09 21:45:00',
            completedAt: '2023-03-09 21:45:00',
          },
        ],
      },
      {
        labelId: 3,
        labelName: '学习',
        todoItems: [
          {
            todoItemId: 311,
            content: '完成Vue.js课程',
            createdAt: '2023-03-05 13:00:00',
            updatedAt: '2023-03-07 15:20:00',
            completedAt: '2023-03-07 15:20:00',
          },
        ],
      },
    ],
  }
})

// 更新已完成待办项的标签排序
Mock.mock('/api/todo-item/update-completed-sort', 'post', (options) => {
  const { labelId, todoItemIds } = JSON.parse(options.body)

  // 验证参数
  if (!labelId || !todoItemIds || !Array.isArray(todoItemIds)) {
    return {
      success: false,
      code: '40001',
      msg: '参数错误',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

// 更新未完成待办项的排序
Mock.mock('/api/todo-item/update-show-sort', 'post', (options) => {
  const { labelId, todoItemIds } = JSON.parse(options.body)

  // 验证参数
  if (!labelId || !todoItemIds || !Array.isArray(todoItemIds)) {
    return {
      success: false,
      code: '40001',
      msg: '参数错误',
      data: null,
    }
  }

  return {
    success: true,
    code: '00000',
    msg: '成功',
    data: null,
  }
})

export default Mock
